package com.zmn.oms.third.taobao;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.google.common.collect.Lists;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.TmallServicecenterWorkerTaglistGetRequest;
import com.taobao.api.response.TmallServicecenterWorkerTaglistGetResponse;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.oms.third.taobao.dto.WorkerAbilityDTO;
import com.zmn.oms.third.taobao.dto.WorkerTagsDTO;
import com.zmn.oms.third.taobao.dto.WorkerTagsResultDTO;
import com.zmn.oms.third.taobao.dto.api.TmallApiInvokResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;

/**
 * 类描述：查询工人标签
 *
 * @author yule
 * @date 2020/04/24 10:25
 */
public class TaobaoPullTaglistHandler {
    private static final Logger logger = LoggerFactory.getLogger(TaobaoPullTaglistHandler.class);

    /**
     * 主动拉取并工人标签  测试类
     *
     * @param isNumber 工人身份证号
     * @param mobile   mobile
     */
    public static TmallApiInvokResult<WorkerTagsDTO> pullMasterTaglist(String isNumber, String mobile, Long masterId) {
        logger.info("【{}】主动拉取工人标签 isNumber:[{}],mobile:[{}]", TaobaoTmcConstants.NAME_TMALL, isNumber, mobile);
        // 查淘宝订单
        TmallApiInvokResult result = TmallApiInvoker.queryWorkerTaglist(isNumber, mobile);
        try {
            if (!result.isSuccess()) {
                logger.info("【{}】主动拉取工人标签失败:[{}]", TaobaoTmcConstants.NAME_TMALL, result.getMsg());
                result.setSuccess(false);
                return result;
            }

            JSONObject jsonObject = JSON.parseObject(result.getMsg());
            jsonObject = jsonObject.getJSONObject("tmall_servicecenter_worker_taglist_get_response");
            WorkerTagsResultDTO workerTagsResultDTO = jsonObject.getObject("result", new TypeReference<WorkerTagsResultDTO>() {
            });

            if (!workerTagsResultDTO.isSuccess()) {
                result.setSuccess(false);
                result.setMsg(workerTagsResultDTO.getMsgInfo());
                logger.info("【{}】主动拉取工人 身份证号：【{}】，手机号【{}】,信息失败:[{}]", TaobaoTmcConstants.NAME_TMALL, isNumber, mobile, result.getMsg());
                return result;
            }

            WorkerTagsDTO workerTagsDTO = workerTagsResultDTO.getResultData();
            workerTagsDTO.setIsNumber(isNumber);
            workerTagsDTO.setMasterId(masterId);
            workerTagsDTO.setMobile(mobile);

            Map<String, Object> provinceListJson = JSON.parseObject(workerTagsDTO.getAbilityMap());
            List<WorkerAbilityDTO> list = Lists.newArrayList();

            provinceListJson.forEach((k, v) -> {
                WorkerAbilityDTO workerAbilityDTO = BeanMapper.map(v, WorkerAbilityDTO.class);
                list.add(workerAbilityDTO);
            });

            workerTagsDTO.setAbilitys(list);
            result.setSuccess(true);
            result.setData(workerTagsDTO);

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            result.setSuccess(false);
            return result;
        }
        return result;
    }


    public static void main(String[] args) throws ApiException {
        String isNumber = "371524199212285815";
        String mobile = "13083271097";

        String token = "6201905d630d0ZZ7c9896520a671383962985ad41f9dd822563261712";
        TaobaoTmcConstants.APPKEY = "27577705";
        TaobaoTmcConstants.SECRET = "7b71e52d7a0cb8d20defff143e74a6f7";
        //TaobaoTmcConstants.API_URL = "http://140.205.164.4/top/router/rest";
        TaobaoTmcConstants.API_URL = "http://gw.api.taobao.com/router/rest";

        TaobaoClient client = new DefaultTaobaoClient(TaobaoTmcConstants.API_URL, TaobaoTmcConstants.APPKEY, TaobaoTmcConstants.SECRET);
        TmallServicecenterWorkerTaglistGetRequest req = new TmallServicecenterWorkerTaglistGetRequest();
        req.setIdNumber(isNumber);
        req.setMobile(mobile);
        TmallServicecenterWorkerTaglistGetResponse rsp = client.execute(req, token);
        System.out.println(rsp.getBody());
    }


}
